package com.umeng.comm.ui.fragments;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.umeng.comm.core.beans.BaseBean;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.ui.imagepicker.fragments.AlbumFragment;
import com.umeng.comm.ui.imagepicker.fragments.BaseFragment;
import com.umeng.comm.ui.imagepicker.presenter.impl.NullPresenter;
import com.umeng.comm.ui.imagepicker.presenter.impl.UserInfoPresenter;

/**
 * Created by summersoft on 2016/5/15.
 */
public class ReleaseFragment extends BaseFragment<Void, NullPresenter> implements View.OnClickListener{

    //列表
    PostedFeedsFragment mPostedFragment = null;

    //我的图册
    AlbumFragment mAlbumFragment;


    //    private ViewPagerIndicator mIndicator;
//    private ViewPager mViewPager;
    //    private String[] mTitles = null;
//    private FragmentPagerAdapter mAdapter;

//    private int mScreenWidth;
//    private int mCurrentTab;
//    private int mTabDistance;

    //列表
    private ImageView mBtnOne;
    private LinearLayout OneLay;
    //相册
    private ImageView mBtnTwo;
    private LinearLayout TwoLay;

    protected CommUser mUser;

    protected UserInfoPresenter mPresenter;

    /**
     * Fragment管理器
     */
    private FragmentManager mFragmentManager = null;
    /**
     * Fragment的parent view,即Fragment的容器
     */
    protected int mFragmentContainer;
    /**
     * 当前显示的Fragment
     */
    public Fragment mCurrentFragment;

    private CommUser userRelase;

    public void setUserRelase(CommUser userRelase) {
        this.userRelase = userRelase;
    }

    @Override
    protected int getFragmentLayout() {
        return ResFinder.getLayout("release_fragment");
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
//        mFragmentManager = getActivity().getSupportFragmentManager();
        mFragmentManager = getChildFragmentManager();
//        mScreenWidth = DeviceUtils.getScreenSize(getActivity()).x;

//        mUser = CommConfig.getConfig().loginedUser;
        mUser = userRelase;
        int btnOneResId = ResFinder.getId("comment_switch_button_one");
        mBtnOne = (ImageView) findViewById(btnOneResId);
        OneLay = (LinearLayout) findViewById(ResFinder.getId("switch_button_one_lay"));
        OneLay.setOnClickListener(this);
//        OneLay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mViewPager.setCurrentItem(0);
//            }
//        });
        int btnTwoResId = ResFinder.getId("comment_switch_button_two");
        mBtnTwo = (ImageView) findViewById(btnTwoResId);
        TwoLay = (LinearLayout) findViewById(ResFinder.getId("switch_button_two_lay"));
        TwoLay.setOnClickListener(this);
        initFragment();
        initTab();
//        TwoLay.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mViewPager.setCurrentItem(1);
//            }
//        });

//        mViewPager = (ViewPager) findViewById(ResFinder.getId("umeng_comm_comment_viewPager"));
//        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int i, float v, int i1) {
//
//            }
//
//            @Override
//            public void onPageSelected(int i) {
////                moveTabCurosr(i);
//                changeSelectedTab(i);
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int i) {
//
//            }
//        });
////        mIndicator.setTabItemTitles(mTitles);
//        mAdapter = new FragmentPagerAdapter(getFragmentManager()) {
//
//            @Override
//            public int getCount() {
//                return 2;
//            }
//
//            @Override
//            public Fragment getItem(int pos) {
//                return getFragment(pos);
//            }
//        };
//        mViewPager.setAdapter(mAdapter);
//        // 设置关联的ViewPager
////        mIndicator.setViewPager(mViewPager, 0);
//

    }

//    /**
//     * 获取对应的Fragment。0：话题聚合 1：活跃用户</br>
//     *
//     * @param pos
//     * @return
//     */
//    private Fragment getFragment(int pos) {
//        if (pos == 0) {
//
//            if (mPostedFragment == null) {
//                mPostedFragment = PostedFeedsFragment.newInstance(mUser);
//                mPostedFragment.setCurrentUser(mUser);
//                mPostedFragment.setOnDeleteListener(new PostedFeedsFragment.OnDeleteListener() {
//
//                    @Override
//                    public void onDelete(BaseBean item) {
//                        mPresenter.decreaseFeedCount(1);
//                    }
//                });
//            }
//            return mPostedFragment;
//
//        } else if (pos == 1) {
//            if (mAlbumFragment == null) {
//                mAlbumFragment = AlbumFragment.newInstance(mUser);
////                mAlbumFragment = new com.umeng.comm.ui.imagepicker.fragments.CommentReceivedFragment();
////                mAlbumFragment.setFeedDetailClassName(mFeedDetailClassName);
////                mAlbumFragment.setTopicDetailClassName(mTopicDetailClassName);
////                mAlbumFragment.setUserInfoClassName(mUserInfoClass);
//            }
//            return mAlbumFragment;
//        }
//        return null;
//    }

    private void changeSelectedTab(int i) {
        OneLay.setSelected(i == 0);
        mBtnOne.setSelected(i == 0);
        TwoLay.setSelected(i == 1);
        mBtnTwo.setSelected(i == 1);

    }

    private void initTab() {
        changeSelectedTab(0);
    }

    public void initFragment() {
        if (mPostedFragment == null){
            mPostedFragment = PostedFeedsFragment.newInstance(mUser);

            mPostedFragment.setCurrentUser(mUser);
            mPostedFragment.setOnDeleteListener(new PostedFeedsFragment.OnDeleteListener() {

                @Override
                public void onDelete(BaseBean item) {
                    mPresenter.decreaseFeedCount(1);
                }
            });
            addFragment(ResFinder.getId("umeng_release_fragment_container"),
                    mPostedFragment);
        }

    }

    /**
     * @param container 用于放置fragment的布局id
     * @param fragment  要添加的Fragment
     */
    public void addFragment(int container, Fragment fragment) {

        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (!isFragmentAdded(fragment)) {
        fragmentTransaction
                .add(container, fragment,
                        fragment.getClass().getName()).commitAllowingStateLoss();
        mCurrentFragment = fragment;
        } else {
            fragmentTransaction
                    .add(container, fragment,
                            fragment.getClass().getName()).commitAllowingStateLoss();
//            fragmentTransaction.show(fragment).commitAllowingStateLoss();
        }

        mFragmentContainer = container;

    }


    /**
     * 判断一个Fragment是否已经添加
     *
     * @param fragment 要判断是否已经添加的Fragment
     * @return
     */
    public boolean isFragmentAdded(Fragment fragment) {
        return fragment != null
                && mFragmentManager.findFragmentByTag(fragment.getClass().getName()) != null;
    }
    /**
     * 显示Fragment，并且把上一个隐藏
     *
     * @param fragmentShow
     */
    public void showFragment(Fragment fragmentShow) {
        showFragmentInContainer(mFragmentContainer, fragmentShow);
    }

    /**
     * 将fragmentShow显示在一个新的container上,而不覆盖mFragmentContainer。
     * 这种情况适用于Fragment中又嵌套Fragment
     *
     * @param container
     * @param fragmentShow
     */
    public void showFragmentInContainer(int container, Fragment fragmentShow) {
        checkContainer();

        if (mCurrentFragment != fragmentShow) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (mCurrentFragment != null) {
                // 首先隐藏原来显示的Fragment
                transaction.hide(mCurrentFragment);
            }
            // 然后再显示传递进来的Fragment
            if (mFragmentManager.findFragmentByTag(fragmentShow.getClass().getName()) == null) {
                transaction
                        .add(container, fragmentShow, fragmentShow.getClass().getName());
            } else {
                transaction.show(fragmentShow);
            }
            transaction.commitAllowingStateLoss();
            mCurrentFragment = fragmentShow;
        }
    }

    /**
     * 检查放置fragment的布局id
     */
    private void checkContainer() {
        if (mFragmentContainer <= 0) {
            throw new RuntimeException(
                    "在调用replaceFragment函数之前请调用setFragmentContainerId函数来设置fragment container id");
        }
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (id == ResFinder.getId("switch_button_one_lay")) {// 已发消息
            if (mCurrentFragment instanceof PostedFeedsFragment) { // 已经处于当前页面，判断是否需要滚动到起始位置
                mPostedFragment.executeScrollToTop();
            } else {
                showFragment(mPostedFragment);
            }

            changeSelectedTab(0);
        } else if (id == ResFinder.getId("switch_button_two_lay")) {// 关注用户
            if (mAlbumFragment == null) {
                mAlbumFragment = AlbumFragment.newInstance(mUser);
                if (mFragmentManager.findFragmentByTag(mAlbumFragment.getClass().getName()) != null){//底部菜单切换的时候，要清除
                    transaction.remove(mAlbumFragment);
                        mAlbumFragment = AlbumFragment.newInstance(mUser);
                    transaction
                            .add(mFragmentContainer, mAlbumFragment, mAlbumFragment.getClass().getName());

                    transaction.commitAllowingStateLoss();

                    mCurrentFragment = mAlbumFragment;
                }else {
                    showFragment(mAlbumFragment);
                }
            }else {
                transaction.remove(mAlbumFragment);

                if (mFragmentManager.findFragmentByTag(mAlbumFragment.getClass().getName()) == null) {
                    transaction
                            .add(mFragmentContainer, mAlbumFragment, mAlbumFragment.getClass().getName());
                } else {
                    mAlbumFragment = AlbumFragment.newInstance(mUser);
                    transaction
                            .add(mFragmentContainer, mAlbumFragment, mAlbumFragment.getClass().getName());
                }
                transaction.commitAllowingStateLoss();

                mCurrentFragment = mAlbumFragment;
            }
            changeSelectedTab(1);
        }
    }


    /**
     * 切换到粉丝时，没有及时清空下面层级的fragment
     */
    public void removeAllFragment(){
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        if (mPostedFragment != null){
            transaction.remove(mPostedFragment);
        }
        if (mAlbumFragment != null){
            transaction.remove(mAlbumFragment);
        }
        transaction.commitAllowingStateLoss();
    }
}
